In this project, we’ll use the Myduino AIoT Education Kit, a sound sensor, a blue LED, an LCD, and WS2812 LEDs to build a ESP32 Clap Switch with Sound Bar Display. You’ll be able to toggle an LED with your clap and also see a real-time sound level visualization on the WS2812 LED strip. The LCD will display the system status, making this project both fun and educational.
Perfect for students, makers, or IoT beginners who want to explore sound detection + visual feedback with ESP32. Let’s go from zero to hero!
Objective
In this project, you’ll learn how to:
- Detect claps with a sound sensor and toggle an LED.
- Visualize real-time sound levels with a WS2812 LED strip.
- Display LED status and sound percentage on an I2C LCD.
- Understand how sensors, outputs, and microcontrollers work together in an interactive system.
By the end, you’ll have your own Clap LED System running on Myduino AIoT Education Kit!
Circuit Connections

| Components | ESP32 Dev Module |
| Blue LED | IO2 |
| Sound Sensor | IO27 |
| LCD 16×2 I2c | SDA = SDA, SCL = SCL |
| RGB / WS2812 | IO4 |
Logic Flow
- The sound sensor detects a sound.
- If the sound level exceeds the clap threshold, the ESP32 toggles the blue LED.
- The WS2812 strip continuously shows a sound level visualization and act like a sound bar.
- The LCD displays system status: “LED ON/OFF” and sound level in %.

Code Lab
Step 1: Install Library
Before upload the code, you need to install these libraries in Arduino IDE:
- FastLED.h by Daniel Garcia (for WS2812 strip)
- LiquidCrystal_I2C.h by Frank de Brabander (for LCD)
- WiFi.h (already include in ESP32, no need to download)

Step 2: Code
Copy and paste this code into Arduino IDE
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
// Pin definitions
#define SOUND_SENSOR_PIN 27 // Analog pin for sound sensor
#define BLUE_LED_PIN 2 // Digital pin for blue LED
#define WS2812_PIN 4 // Digital pin for WS2812 strip
#define NUM_LEDS 6 // Number of LEDs in WS2812 strip
// LCD setup (I2C address 0x27, 16x2)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// WS2812 setup
CRGB leds[NUM_LEDS];
// Variables for clap detection
bool ledState = false;
int soundThreshold = 100; // Adjust based on sensor sensitivity
int clapThreshold = 200; // Higher threshold for clap detection
unsigned long lastClapTime = 0;
unsigned long clapDebounce = 500; // Minimum time between claps (ms)
// Variables for sound visualization
int soundLevel = 0;
int maxSoundLevel = 1023; // Maximum ADC reading
unsigned long lastDisplayUpdate = 0;
unsigned long displayInterval = 100; // Update display every 100ms
void setup() {
Serial.begin(115200);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(SOUND_SENSOR_PIN, INPUT);
// LCD init
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Clap LED System");
lcd.setCursor(0, 1);
lcd.print("LED: OFF");
// WS2812 init
FastLED.addLeds<WS2812, WS2812_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
FastLED.clear();
FastLED.show();
digitalWrite(BLUE_LED_PIN, LOW);
delay(2000); // Initial message delay
}
void loop() {
soundLevel = analogRead(SOUND_SENSOR_PIN);
detectClap();
updateSoundVisualization();
if (millis() - lastDisplayUpdate > displayInterval) {
updateDisplay();
lastDisplayUpdate = millis();
}
delay(10);
}
void detectClap() {
unsigned long currentTime = millis();
if (soundLevel > clapThreshold && (currentTime - lastClapTime) > clapDebounce) {
ledState = !ledState;
digitalWrite(BLUE_LED_PIN, ledState ? HIGH : LOW);
lastClapTime = currentTime;
flashClapIndication();
Serial.println("Clap detected! LED toggled.");
}
}
void updateSoundVisualization() {
int numLedsToLight = map(soundLevel, soundThreshold, maxSoundLevel, 0, NUM_LEDS);
numLedsToLight = constrain(numLedsToLight, 0, NUM_LEDS);
FastLED.clear();
for (int i = 0; i < numLedsToLight; i++) {
if (i < NUM_LEDS / 3) {
leds[i] = CRGB::Green; // First part = Green
} else if (i < 2 * NUM_LEDS / 3) {
leds[i] = CRGB::Yellow; // Middle part = Yellow
} else {
leds[i] = CRGB::Red; // Last part = Red
}
}
FastLED.show();
}
void flashClapIndication() {
for (int i = 0; i < NUM_LEDS; i++) leds[i] = CRGB::White;
FastLED.show();
delay(100);
updateSoundVisualization();
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Clap LED System");
lcd.setCursor(0, 1);
lcd.print("LED: ");
lcd.print(ledState ? "ON " : "OFF");
lcd.setCursor(8, 1);
lcd.print(" S:");
lcd.print(map(soundLevel, 0, 1023, 0, 100));
lcd.print("%");
}
Step 3: Upload and Run
- Connect your ESP32 board
- Upload the code
- Open Serial Monitor at 115200 baud
- Try clapping near the sound sensor
System Check
When working properly:
- A clap toggles the blue LED ON/OFF
- WS2812 strip flashes when clap detected
- LCD shows “LED ON/OFF” + sound percentage
Troubleshooting Guide
| Problem | Solution |
| No reaction to claps | Adjust clapThreshold (make it lower for sensitive) |
| LCD not displaying any text | Confirm the SDA connect to SDA and SCL connect to SCL |
| Serial Monitor shows nothing | Check the sound sensor pin is connected to GPIO 27 |
| LED toggle randomly | Increase clapDebounce or threshold |
Tips: If you want to find any lines in Arduino IDE, click CTRL+F (CMD + F on Mac) and search the line you want to find (applicable to any platform).
Buy from:
Myduino AIoT Education Kit from Myduino.com






